package com.hcc.gateway.ops.config;

import com.hcc.gateway.domain.resp.RestResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理
 *
 * @author hushengjun
 * @date 2022/1/9
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(IllegalArgumentException.class)
    public RestResp<String> illegalArgumentExceptionHandler(HttpServletRequest req, IllegalArgumentException ex) {
        log.error("参数异常", ex);
        return RestResp.failed(ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public RestResp<String> exceptionHandler(HttpServletRequest req, Exception ex) {
        log.error("未知异常", ex);
        return RestResp.failed("请求出错了！请稍后再试");
    }

}
